//
//  PageViewController.m
//  Lightspeed
//
//  Created by iD Student on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PageViewController.h"
#import "InternetBrowserViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface PageViewController ()

@end

@implementation PageViewController
@synthesize pageControl;
@synthesize scrollView;
@synthesize internet;
@synthesize webViewsArray;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor darkGrayColor];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize=CGSizeMake(0, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate=self;
    pages=0;
    pageControl.numberOfPages=0;
    if(!webViewsArray)
        webViewsArray=[[NSMutableArray alloc] init];
    [super viewDidLoad];
    
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setPageControl:nil];
    [self setScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) dismissAndSetView:(id) sender{
    
    [self dismissModalViewControllerAnimated:YES];
    UIWebView *view=[webViewsArray objectAtIndex:0];
    //[internet.webView addSubview:[webViewsArray objectAtIndex:0]];
    if(view.request.URL.absoluteString == nil)
        [internet.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    else {
        [internet.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:view.request.URL.absoluteString]]];
    }
   
}

- (IBAction)addView:(id)sender 
{
    
    UIWebView *webView1 = [[UIWebView alloc] initWithFrame:CGRectMake(75+scrollView.contentSize.width, 75, 170, 200)];
    [webViewsArray addObject:webView1];
    UIButton *webButton=[[UIButton alloc] initWithFrame:CGRectMake(75+scrollView.contentSize.width, 75, 170, 200)];
    [webButton addTarget:self action:@selector(dismissAndSetView:) forControlEvents:UITouchPhaseEnded];
    [webView1 addSubview:webButton];
    [scrollView addSubview:webView1];
    scrollView.contentSize=CGSizeMake(scrollView.contentSize.width+320, 350);
    pages++;
    [pageControl setNumberOfPages:pages];
    
 
    



}



- (void)scrollViewDidScroll:(UIScrollView *)sender {

    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
    
}

@end
